var page= 0;
var start= 0;
var batchSize=10;
var countInTable=0;
var view1 = [
    {cells: [[
            {name: '用户名', field: "username"},
            {name: '邮箱', field: "mailaddress"},
            {name: '所在分公司',field: "subcompany"},
            {name: '备注',field: "memo"}
        ]]}
    ];

var layout = view1;
var searchuserlayout = [{
    field: 'username',
    name: '用户名',
    width: '100px'
},
{
    field: 'mailaddress',
    name: '邮箱',
    width: '160px'
},
{
    field: 'subcompany',
    name: '所在分公司',
    width: '150px'

},
{
    field: 'memo',
    name: '备注',
	type: dojox.grid.cells.DateTextBox,
    width: '130px'

}];

var searchsubcompanylayout = [
{
    field: 'name',
    name: '分公司名',
    width: '150px'
},
{
    field: 'address',
    name: '地址',
    width: '180px'
},
{
    field: 'memo',
    name: '备注',
    width: 'auto'

}];

var searchResumelayout = [
{
    field: 'username',
    name: '姓名',
    width: '50px'
},
{
    field: 'address',
    name: '地址',
    width: '100px'
},
{
    field: 'citylivein',
    name: '现居住城市',
    width: '80px'
},
{
    field: 'contactWay',
    name: '联系方式',
    width: '80px'
},
{
    field: 'degree',
    name: '学位',
    width: '80px'
},
{
    field: 'fitposition',
    name: '适合职位',
    width: '80px'
},
{
    field: 'graduateDate',
    name: '毕业时间',
    width: '80px'
},
{
    field: 'graduateSchool',
    name: '毕业院校',
    width: '80px'
},
{
    field: 'mailBox',
    name: '邮箱地址',
    width: '80px'
},
{
    field: 'major',
    name: '专业',
    width: '80px'
},
{
    field: 'oldCompany',
    name: '原公司',
    width: '80px'
},
{
    field: 'oldPosition',
    name: '原职位',
    width: '80px'
},
{
    field: 'workExperience',
    name: '工作经验',
    width: '80px'
}
];

function handleError(){
    dojo.debug("No data returned");
}
function next() {
    page =page + 1;
    searchuserstore.fetch({query: {}, onBegin: size, start: 0, count: batchSize});
    alert(countInTable);
    alert(page);
    if(countInTable < batchSize) {
    	page = page - 1;
    }
    loadTable();
}

function previous() {
    page =page - 1;
    if (page < 0) page = 0;
    loadTable();
}
//================begin of user Request ==============
function searchUserForTable(){
    var targetURL ="searchbyname.action?username="+encodeURIComponent(searchuser_username.value);
    searchuserstore = new dojo.data.ItemFileReadStore({url:targetURL});
    searchuserstore.close();
    searchusergrid.setStore(searchuserstore);
}
function saveUserForTable(){
    var xhrArgs = {
  	       url: "saveuserinfo.action",
	       form: dojo.byId("sendsaveuserform"),
           error: function(error) {
           	 dojo.byId("response").innerHTML = "An unexpected error occurred: " + error;
           }
        };
    var deferred = dojo.xhrPost(xhrArgs);
}
function deleteUserForTable(selectedItem){
    var targetURL ="deleteuserinfo.action?username="+encodeURIComponent(selectedItem.username);
    var xhrArgs = {
   	       url: targetURL,
 	       //handleAs: "text",
 		   //load: function(response, ioArgs) {
 	       //    dojo.byId("response").innerHTML ="";
 	       //    dojo.byId("response").innerHTML = response;
 	       //    dojo.parser.parse("response");
 		   //},
           error: function(error) {
                dojo.byId("response").innerHTML = "An unexpected error occurred: " + error;
           }
    };
    //dojo.byId("response").innerHTML = "loading...";
    var deferred = dojo.xhrPost(xhrArgs);
}
//================end of user Request ==============


//================begin of SubCompany Request ==============
function addsubcompany(){
    var xhrArgs = {
   	       url: "registersubcpy.action",
 	       form: dojo.byId("registersubcompany"),
 		   load: function(response, ioArgs) {
    	     addsubcpyfCmDialog.show();
    	     dojo.byId("systemMessage").innerHTML="";
 		   },
           error: function(error) {
             dojo.byId("response").innerHTML = "An unexpected error occurred: " + error;
           }
         };
     dojo.byId("systemMessage").innerHTML = "正在提交添加请求...";
     var deferred = dojo.xhrPost(xhrArgs);
}
function saveSubcompanyForTable(){
    var xhrArgs = {
  	       url: "savesubcpyinfo.action",
	       form: dojo.byId("sendsavesubcompanyform"),
           error: function(error) {
           	 dojo.byId("response").innerHTML = "An unexpected error occurred: " + error;
           }
        };
    var deferred = dojo.xhrPost(xhrArgs);
}
function deleteSubcompanyForTable(selectedItem){
    var targetURL ="deletesubcpyinfo.action?name="+encodeURIComponent(selectedItem.name);
    var xhrArgs = {
   	       url: targetURL,
 		  // load: function(response, ioArgs) {
	      //   addsubcpyfCmDialog.show();
	      //   dojo.byId("systemMessage").innerHTML="";
 		  // },
           error: function(error) {
                dojo.byId("response").innerHTML = "An unexpected error occurred: " + error;
           }
    };
    //dojo.byId("response").innerHTML = "loading...";
    var deferred = dojo.xhrPost(xhrArgs);
}
function searchCompanyForTable(){
	alert(searchcpy_cpyname.value);
    var targetURL ="searchbycpyname.action?name="+encodeURIComponent(searchcpy_cpyname.value);
    searchsucompanystore = new dojo.data.ItemFileReadStore({url:targetURL});
    searchsucompanystore.close();
    searchsucompanygrid.setStore(searchsucompanystore);
}
//================end of SubCompany Request ==============

//================begin of Resume Request ==============
function saveResumeForTable(){
    var xhrArgs = {
  	       url: "saveresume.action",
	       form: dojo.byId("sendsaveresumeform"),
           error: function(error) {
           	 dojo.byId("response").innerHTML = "An unexpected error occurred: " + error;
           }
        };
    var deferred = dojo.xhrPost(xhrArgs);
}
function deleteResumeForTable(selectedItem){
    var targetURL ="deleteresume.action?id="+encodeURIComponent(selectedItem.id);
    var xhrArgs = {
   	       url: targetURL,
 		  // load: function(response, ioArgs) {
	      //   addsubcpyfCmDialog.show();
	      //   dojo.byId("systemMessage").innerHTML="";
 		  // },
           error: function(error) {
                dojo.byId("response").innerHTML = "An unexpected error occurred: " + error;
           }
    };
    //dojo.byId("response").innerHTML = "loading...";
    var deferred = dojo.xhrPost(xhrArgs);
}

//================end of Resume Request ==============



function loadTable(){
    start = page;
    var targetURL = "searchuser.action?currentPage="+ encodeURIComponent(start);
    searchuserstore = new dojo.data.ItemFileReadStore({url:targetURL});
    searchuserstore.close();
    searchusergrid.setStore(searchuserstore);
}
function size(size, request) {
	countInTable=size;
	  //Do whatever with the size var.
}

function sendaddresume(){
	  dojo.io.iframe.send({
	      url: "addresumeinfo.action",
	      form: dojo.byId("sendaddresumeform"),
	      handleAs: "text",
	      load: function(response, ioArgs) {
            dojo.byId("response").innerHTML ="";
            dojo.byId("response").innerHTML = response;
            dojo.parser.parse("response");
	      },
	      error: function(error){
         	 dojo.byId("response").innerHTML = "An unexpected error occurred: " + error;
	      }
	  });
}

